﻿using Microsoft.AspNetCore.SignalR;
using Redbus.Events;
using Redbus.Interfaces;
using WCS.Entities;
using WCS.Services;

namespace WCS.LongTaskService
{
    public class MessageHandlerService : BackgroundService
    {
        private readonly IHubContext<ReportHub> hubContext;
        private readonly IEventBus eventBus;

        public MessageHandlerService(IHubContext<ReportHub> hubContext, IEventBus eventBus)
        {
            this.hubContext = hubContext;
            this.eventBus = eventBus;
        }

        protected override Task ExecuteAsync(CancellationToken stoppingToken)
        {

            eventBus.Subscribe<PayloadEvent<WarningInfo>>(OnWarningEvent);
            eventBus.Subscribe<PayloadEvent<string>>(OnUpdateEvent);



            return Task.CompletedTask;

        }


        private async void OnUpdateEvent(PayloadEvent<string> payloadEvent)
        {
            await hubContext.Clients.All.SendAsync("ChangeStatus", "");
        }

        private async void OnWarningEvent(PayloadEvent<WarningInfo> payloadEvent)
        {
            await hubContext.Clients.All.SendAsync("WarningInfo", payloadEvent.Payload);
        }
    }
}
